<template>
  <el-drawer
    :title="drawerTitle[drawerType]"
    size="60%"
    :visible.sync="drawerVisible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="clearDialog"
    custom-class="tag-drawer user-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
          <h4 class="type-title">
            <div class="icon-box"><icon-svg name="iconBase"></icon-svg></div>
            <span>基本信息</span>
          </h4>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="字典名称" prop="dictNm">
                <el-input placeholder="请输入字典名称" :readonly="!editable" v-model="editForm.dictNm"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="字典类型" prop="dictType">
                <el-input
                  placeholder="字典类型为字典的英文标识"
                  :readonly="!editable"
                  v-model="editForm.dictType"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注信息" prop="cmntStr">
                <el-input
                  type="textarea"
                  placeholder="请输入备注信息，最大不能超过100个字符"
                  v-model="editForm.cmntStr"
                  :rows="3"
                  resize="none"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="clearDialog">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  data() {
    return {
      editable: true,
      drawerVisible: false,
      drawerType: 'add',
      drawerTitle: {
        add: '新增',
        edit: '编辑'
      },
      dictId: '',
      editForm: {
        dictNm: '',
        dictType: '',
        dictStatus: 0,
        cmntStr: ''
      },
      editFormRules: {
        dictNm: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' },
          { max: 20, message: '字典名称不能超过20个字符', trigger: 'blur' }
        ],
        dictType: [
          { required: true, message: '字典类型不能为空', trigger: 'blur' },
          { max: 30, message: '字典类型不能超过30个字符', trigger: 'blur' },
          { validator: this.$validator(/^[0-9a-zA-Z_]{1,}$/, '字典类型只能输入字母数字和下划线'), trigger: 'blur' }
        ],
        cmntStr: [{ max: 100, message: '备注信息不能超过100个字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showDrawer(type, data) {
      this.drawerType = type
      switch (type) {
        case 'add':
          this.editable = true
          this.drawerVisible = true
          break
        case 'edit':
          this.editable = false
          this.getDetail(data)
          break
      }
    },
    submit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          let params = {
            dictNm: this.editForm.dictNm,
            dictType: this.editForm.dictType,
            cmntStr: this.editForm.cmntStr
          }
          if (this.drawerType === 'edit') {
            params = Object.assign({}, params, { dictId: this.dictId, dictStatus: this.editForm.dictStatus })
          }
          this.$ajax({
            url: this.$ajax.setUrl('sys/sysClientDictMgr'),
            method: this.drawerType === 'add' ? 'post' : 'put',
            data: this.$ajax.setData(params)
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.clearDialog()
              this.getList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    getDetail(scope) {
      this.$ajax({
        url: this.$ajax.setUrl('sys/sysClientDictMgr/' + scope.dictId),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.dictId = data.data.dictId || ''
          this.editForm.dictNm = data.data.dictNm || ''
          this.editForm.dictType = data.data.dictType || ''
          this.editForm.dictStatus = data.data.dictStatus
          this.editForm.cmntStr = data.data.cmntStr || ''
          this.drawerVisible = true
        } else {
          this.$message('获取字典信息失败！')
        }
      })
    },
    clearDialog() {
      this.dictId = ''
      this.editForm.dictNm = ''
      this.editForm.dictType = ''
      this.editForm.dictStatus = ''
      this.editForm.cmntStr = ''
      this.$nextTick(() => {
        this.$refs['editForm'].clearValidate()
      })
      this.drawerVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
